package com.david.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.david.entity.Order;
import com.david.service.SecKillService;
import com.david.vo.ResponseBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description: 秒杀入口
 * @create: 2021-04-25-15-38
 * @author: David
 **/
@RestController
public class SecKillController {

    private static Integer num = 0;

    @Resource
    HttpServletRequest request;

    @Resource
    SecKillService secKillService;

    /**
     * 秒杀请求的地址，userId表示用户身份，orderId表示请求的商品id
     */
    @GetMapping("/secKill")
    public ResponseBean secKill() throws Exception {
        // 每个请求一个number，如果num相同则默认为同一个用户的多次请求
        StpUtil.setLoginId(num++);

        Order order = new Order();
        order.setOrderId("1");
        order.setUserId(StpUtil.getLoginIdAsString());

        return secKillService.secKill(order);
    }

    /**
     * 秒杀查询入口
     * @param id 查询的商品id
     * @return 返回时间戳或者秒杀的地址
     */
    @GetMapping("/entrance")
    public ResponseBean entrance(@RequestParam(name = "id", required = true) Integer id) {
        return secKillService.entrance(id);
    }

}
